package demo.IOInputOutput;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/15-08-15-11:19
 * @Description：demo.IOInputOutput   对象流
 */
/*
对象流(字节结点包装流)
    需要实现Serializable接口 否则抛出NotSerializableException 没有序列化异常
    序列化
        把对象以流的方式，写入到文件中保存，为对象的序列化
        writeObject(Object obj)
    反序列化
        把文件中保存的对象，以流的方式读取出来，
        反序列化破坏单例模式，反序列化的对象和原对象地址不相等，
        反序列化创建对象并不走构造器

    transient 修饰的变量不参与序列化

    每次修改类的定义，都会给class文件生成一个新的序列化
    static final long serialVersionUID = 12L;       确保无论是否对类的定义进行修改，都不重新生成新序列号
 */
public class Object_Stream {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            Student stu = new Student(18,"tom2",2);
            oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
            //System.out.println(stu);
            Set<Student> stus = new HashSet<>();
            stus.add(stu);
            stus.add(new Student(19,"tom",3));
            stus.add(new Student(12,"Aim",4));
            stus.add(new Student(3,"Lucky",5));
            stus.add(new Student(7,"John",6));
            oos.writeObject(stus);
            oos.flush();

            ois = new ObjectInputStream(new FileInputStream("a.txt"));

            stus = (Set<Student>) ois.readObject();
            for (Student student : stus) {
                System.out.println(student);
            }
//            while((stu = (Student) ois.readObject()) != null){      //错误写法
//                System.out.println(stu);
//            }
//            stu = (Student) ois.readObject();
//            System.out.println(stu);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(oos != null){
                    oos.close();
                }
                if(ois != null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
class Student implements Serializable {
    static final long serialVersionUID = 12L;
    private int age;
    private String name;
    transient private int id;      //不会出现序列化
    private String password;

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Student() {
    }

    public Student(int age, String name, int id) {
        this.age = age;
        this.name = name;
        this.id = id;
    }
}
